package edu.mtu.citizenscience.cs.notepad.activity.noteslist.note;

import java.io.IOException;

import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;

import edu.mtu.citizenscience.cs.notepad.NPActivity;
import edu.mtu.citizenscience.cs.notepad.activity.R;
import edu.mtu.citizenscience.cs.notepad.activity.noteslist.note.recording.RecordActivity;
import edu.mtu.citizenscience.cs.notepad.model.ModelNote;
/*
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Recording Activity Responsible for playing, adding
 * and removing recording from the note.
 */
public class RecordingActivity extends NPActivity {
	// The model of the note the recording gets attached to
	private ModelNote note;
	// the android media player for playing back the recording
	private MediaPlayer mediaPlayer;
	
	/**
	 * Recording Activity creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.activity_recording);
		
		// try to get the note id from the intent bundle
		long noteId = this.getIntent().getLongExtra(ModelNote.NOTE_ID, -1);
		
		// check to see if got a note id
		if (noteId > -1)
		{
			// set the note model to the model with the given note id
			this.note = new ModelNote(this, noteId).find();
		}
		else
		{
			// no note to attach the recording to so finish the activity
			this.finish();
		}
		
		// set play button click handler to play recording
		this.aq.id(R.id.playRecordingButton).clicked(this, "playRecording");
	}
	
	/**
	 * Recording Activity resume handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	public void onResume()
	{
		super.onResume();
		
		// check if the note has a recording attached
		if (this.note.getRecording().getId() == -1)
		{
			// hide the attached recording display
			this.hideRecording();
		}
		else
		{
			// display the attached recording
			this.displayRecording();
		}
	}
	
	/**
	 * Recording Activity pause handler
	 * 
	 * @param   void
	 * @return	void
	 */
	@Override
	public void onPause() 
	{
		super.onPause();
		
		// check if the android media player is attached
		if (this.mediaPlayer != null)
		{
			// stop the media player
			this.mediaPlayer.stop();
			// release the media player from the app
			this.mediaPlayer.release();
		}
	}
	
	/**
	 * Display the notes attached recording
	 * 
	 * @param   void
	 * @return	void
	 */
	public void displayRecording()
	{
		// set the play button as visible
		this.aq.id(R.id.playRecordingButton).visible();
		
		// set the recording button text to the remove label and set click handler to removeRecording
		this.aq.id(R.id.attachmentRecordingButton).text(R.string.remove_button_label).clicked(this, "removeRecording");
	}
	
	/**
	 * Get a new recording for this note
	 * 
	 * @param   void
	 * @return	void
	 */
	public void getRecording()
	{
		// launch record activity passing the note id
		this.startActivity(new Intent(this, RecordActivity.class).putExtra(ModelNote.NOTE_ID, this.note.getId()));
	}
	
	/**
	 * Play the notes recording
	 * 
	 * @param   void
	 * @return	void
	 */
	public void playRecording()
	{
		try 
		{
			// get new media player instance
			this.mediaPlayer = new MediaPlayer();
			// set the stream type to music (means long play audio file somebody listens to)
	        this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
	        // set the media player's data source as the note recording's file path
			this.mediaPlayer.setDataSource(this.note.getRecording().getPath());
			// prepare the media player for playback
			this.mediaPlayer.prepare();
			// start the media player playing the audio
			this.mediaPlayer.start();
		}
		catch (IllegalArgumentException e) 
		{
			e.printStackTrace();
		}
		catch (SecurityException e) 
		{
			e.printStackTrace();
		}
		catch (IllegalStateException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * Remove the note's recording
	 * 
	 * @param   void
	 * @return	void
	 */
	public void removeRecording()
	{
		// delete the recording file
		this.note.getRecording().getFile().delete();
		// delete the recording model from the DB
		this.note.getRecording().delete();
		// save the note
		this.note.save();
		
		// hide the recording display
		this.hideRecording();
	}
	
	/**
	 * Hide the note recording display
	 * 
	 * @param   void
	 * @return	void
	 */
	public void hideRecording()
	{
		// hide the recording play button
		this.aq.id(R.id.playRecordingButton).invisible();
		
		// set the recording button text to the attach label and set the clicked handler to getRecording
		this.aq.id(R.id.attachmentRecordingButton).text(R.string.attach_button_label).clicked(this, "getRecording");
	}
	
}